package com.hxl.tech.gateway.auth.controller;

import com.hxl.tech.gateway.auth.annotate.ApiPrivilege;
import com.hxl.tech.gateway.auth.dto.Response;
import com.hxl.tech.gateway.auth.dto.openapi.*;
import com.hxl.tech.gateway.auth.model.OpenApiApproval;
import com.hxl.tech.gateway.auth.service.OpenApiApprovalService;
import com.hxl.tech.gateway.auth.util.CopyUtil;
import com.hxl.tech.gateway.common.constant.AppConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * 开放接口授权 Controller
 * @author soliddq
 * @date 2023-10-10
 */
@Api(value = "接口授权管理", tags = "接口授权管理")
@RestController
@RequiredArgsConstructor
public class OpenApiApprovalController {

    private final OpenApiApprovalService openApiApprovalService;

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.api.approval.find")
    @ApiOperation(value = "查询接口授权列表", response = OpenApiApprovalDTO.class)
    public Response findApiApproval(@RequestBody @Validated OpenApiApprovalFindDTO findDTO) {

        return Response.createSuccess(CopyUtil.to(openApiApprovalService.find(findDTO.getAppId()), OpenApiApprovalDTO.class));
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.api.approval.save")
    @ApiOperation(value = "新增开放接口", response = OpenApiApprovalDTO.class)
    public Response saveApiApproval(@RequestBody @Validated OpenApiApprovalSaveDTO saveDTO) {

        openApiApprovalService.save(CopyUtil.to(saveDTO, OpenApiApproval.class));
        return Response.createSuccess();
    }

    @ApiPrivilege(needRole = AppConstant.ROLE_ADMIN)
    @PostMapping("gateway.auth.api.approval.delete")
    @ApiOperation(value = "删除开放接口", response = String.class)
    public Response deleteApiApproval(@RequestBody @Validated OpenApiApprovalDeleteDTO deleteDTO) {

        openApiApprovalService.delete(CopyUtil.to(deleteDTO, OpenApiApproval.Id.class));
        return Response.createSuccess();
    }
}
